namespace :js do
  desc <<-eos
          Run MooTools in a private sandbox to preserve the global namespace. 
          This task ensures that only specific MooTools objects (i.e: Request, Element, etc)
          are made globally avaliable.
          
          To define what objects from MooTools are exported, modify the following
          file which resides in the namespace:
              <libs/mootools/exports>
              
          For example, to export the 'Element' object from MooTools to the 
          window, enter the javascript statement:
              window.Events = Events;
          
          It's important that 'Class' is not exported as 'Class'. There
          is already a Class object in the global namespace. See:
              <lang/Class>
       eos
  task :moo => :environment do
    libs = [
      'public/javascripts/application/library/libs/mootools/moo1.2.js',
      'public/javascripts/application/library/libs/mootools/exports.js',
    ]
    runtime_export = 'public/javascripts/application/library/libs/mootools/moo1.2.merged.js'

    _stream = File.open(runtime_export,"w+")
    _stream.write("(function(){\r")
      libs.each {|lib| open(lib) {|f| _stream.write("\r\r#{f.read}") } }
    _stream.write("\r})();") 
    puts "    ---> Created: libs/mootools/moo1.2.merged.js\r"
  end
end

